<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/main.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/infotable.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/datatable.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/jquery-ui/jquery-ui.min.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/api.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.dataTables.chs.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-ui/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
    
    var urlPrefix = "${pageContext.request.contextPath}/api/";
    var tenderId = "<s:property value='tenderId' />";
    var memberLoginName = "<s:property value='memberInfo.memberLoginName' />";
    var lastestAuditPhase = "<s:property value='memberInfo.lastestAuditPhase' />";
    var tenderAPI = new TenderAPI({urlPrefix : urlPrefix});
    var tenderData = {};
    
    if (tenderId == "") {
        window.location.href = "${pageContext.request.contextPath}/forward/home";
    }
    
    tenderAPI.detail({
		onSuccess : function(data) {
		    tenderData = data.tender;
		    $('title').text(tenderData.tender_title);
		    $('#lbl_tender_title').text(tenderData.tender_title);
			initTenderInfo();		    
		    checkButtonStatus();
		    initRemainingTimer();
		},
		onAPIError : function(status, errors) {
		    window.location.href = "${pageContext.request.contextPath}/forward/home";
		},
		onServerError : function(jqXHR, textStatus, errorThrown) {
		    window.location.href = "${pageContext.request.contextPath}/forward/home";
		}
	},
	{
		param: JSON.stringify({
		    tender_id: tenderId,
		    member_login_name: memberLoginName
		})
	});
    
    $('#btn_attend').click(function() {
        if (memberLoginName == "") {
            window.location.href = "${pageContext.request.contextPath}/forward/login";
        } else {
            if (tenderData.tender_pattern == 2) {
                tenderAPI.searchInvite({
          	        onSuccess : function(data) {
          	        	if (data.status == 0) {
          	        	    selectedVender = data.members;
          	        	    var exists = false;
          	        	    if (selectedVender.length > 0) {
                                $.each(selectedVender, function(index, row) {
                                    if (row.member_login_name == memberLoginName) {
                                        exists = true;
                                        return false;
                                    }
                                });
                            }
          	        	    if (exists == false) {
          	        	        $.alert('错误', '您不是定向邀标的邀请对象，不能报价！');
          	        	    } else {
          	        	        quote();
          	        	    }
          	        	}	        	
        	        },
        	        onAPIError : function(status, errors) {
        	        },
        	        onServerError : function(jqXHR, textStatus, errorThrown) {
        	        }
        	    },
        	    {
        	        param: JSON.stringify({
        	            tender_id: tenderId
        	        })
        	    });
            } else {
                if (tenderData.status == 1) {
                } else if (tenderData.status == 2) {
                    if (!tenderData.enroll_flag) {
                        enroll();
                    } else if (tenderData.finalist_flag && !tenderData.quote_flag) {
                        quote();
                    }
                } else if (tenderData.status == 3) {
                } else if (tenderData.status == 4 && !tenderData.quote_flag) {
                    quote();
                }
            }
        }
    });
    
    var table = $('#tender_product').DataTable({
		paging: false,
		info: false,
		searching: false,
		jQueryUI: false,
		ordering: false,
		autoWidth: false,
		columns: [
			{data:'product_name', width:'30%', className: 'dt-body-left'},
			{data:'quantity', width:'15%', className: 'dt-body-center'},
			{data:'unit', width:'15%', className: 'dt-body-center'},
			{data:'product_description', width:'40%', className: 'dt-body-left'}
		],
		serverSide: true,
		processing: true,
		ajax: {
			url: '${pageContext.request.contextPath}/api/TenderProductSearchAPI.json',
			type: 'POST',
			data: function(d) {
			    var paramData = {};
			    paramData.tender_id = tenderId;
			    d.param = JSON.stringify(paramData);
			},
			dataSrc: 'products'
		}
	});
    
    function initTenderInfo() {
        $('#tender_title').text(tenderData.tender_title);
	    $('#tender_no').text(tenderData.tender_no);
	    $('#tender_pattern_name').text(tenderData.tender_pattern_name);
	    $('#create_date').text(tenderData.create_date.substr(0, 10));
	    $('#enroll_start_date').text(tenderData.enroll_start_date);
	    $('#enroll_end_date').text(tenderData.enroll_end_date);
	    $('#tender_end_date').text(tenderData.tender_end_date);
	    $('#publish_result_date').text(tenderData.publish_result_date);
	    $('#contact_name').text(tenderData.contact_name);
	    $('#contact_tel').text(tenderData.contact_tel);
	    $('#content_description').html(tenderData.content_description);
	    $('#vendor_requirement').html(tenderData.vendor_requirement);
	    
	    if (tenderData.status <= 3) {
	        $('#vender_count').text(tenderData.enroll_count);
	        $('#action_name').text('报名');
	    } else {
	        $('#vender_count').text(tenderData.quote_count);
	        $('#action_name').text('投标');
	    }
    }
    
    function checkButtonStatus() {
        if (memberLoginName == "") {
            $('#btn_attend').removeClass().addClass('login_notice');
        } else {
            if (tenderData.status == 1) {
                $('#btn_attend').removeClass().addClass('enroll_wait');
            } else if (tenderData.status == 2) {
                if (tenderData.enroll_flag && !tenderData.finalist_flag) {
                    $('#btn_attend').removeClass().addClass('enroll_finish');
                } else if (tenderData.enroll_flag && tenderData.finalist_flag) {
                    if (tenderData.quote_flag) {
                        $('#btn_attend').removeClass().addClass('tender_finish');
                    } else {
                        $('#btn_attend').removeClass().addClass('tender_action');
                    }
                } else {
                    $('#btn_attend').removeClass().addClass('enroll_action');                
                }
            } else if (tenderData.status == 3) {
                $('#btn_attend').removeClass().addClass('enroll_end');
            } else if (tenderData.status == 4) {
                if (tenderData.quote_flag) {
                    $('#btn_attend').removeClass().addClass('tender_finish');
                } else {
                    $('#btn_attend').removeClass().addClass('tender_action');
                }
            } else if (tenderData.status == 5 || tenderData.status == 6) {
                $('#btn_attend').removeClass().addClass('tender_end');
            } else if (tenderData.status == 7) {
                $('#btn_attend').removeClass().addClass('tender_end');
            }
        }
    }
    
    function initRemainingTimer() {
        var remaining = 0;
        if (tenderData.status == 1) {
            remaining = tenderData.enroll_start_remaining;
            $('#timer_type').text("报名开始");
        } else if (tenderData.status == 2) {
            remaining = tenderData.enroll_end_remaining;
            $('#timer_type').text("报名截止");
        } else if (tenderData.status == 3) {
            remaining = 0;
            $('#timer_type').text("报名截止");
        } else if (tenderData.status == 4) {
            remaining = tenderData.tender_end_remaining;
            $('#timer_type').text("投标截止");
        } else if (tenderData.status == 5) {
            remaining = 0;
            $('#timer_type').text("投标截止");
        } else if (tenderData.status == 6) {
            remaining = tenderData.publish_result_remaining;
            $('#timer_type').text("中标结果公布");
        } else if (tenderData.status == 7) {
            remaining = 0;
            $('#timer_type').text("中标结果公布");
        }
        timer(remaining, function() {
            if (tenderData.status == 2) {
                $('#btn_attend').removeClass().addClass('enroll_end');
            } else if (tenderData.status == 4) {
                $('#btn_attend').removeClass().addClass('tender_end');
            }
        });
    }
    
    function timer(intDiff, handler) {
    	var timer = window.setInterval(function() {
        	var day=0,
        		hour=0,
        		minute=0,
        		second=0;
        	if (intDiff > 0) {
        		day = Math.floor(intDiff / (60 * 60 * 24));
        		hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
        		minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
        		second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
        	} else {
        	    if (handler && typeof(handler) == "function") {
        	        handler();
        	        window.clearInterval(timer);
        	    }
        	}
        	if (hour <= 9) hour = '0' + hour;
        	if (minute <= 9) minute = '0' + minute;
        	if (second <= 9) second = '0' + second;
        	$('#timer_day').text(day);
        	$('#timer_hour').text(hour);
        	$('#timer_minute').text(minute);
        	$('#timer_second').text(second);
        	intDiff --;
    	}, 1000);
    }
    
    function enroll() {
        tenderAPI.enroll({
    		onSuccess : function(data) {
    		    if (data.result) {
    		        $.alert('提示', "报名成功！");
    		        $('#btn_attend').removeClass().addClass('enroll_finish');
    		        tenderData.enroll_flag = true;
    		    }
    		},
    		onAPIError : function(status, errors) {
    		    $.alert('错误', errors[0].msg);
    		},
    		onServerError : function(jqXHR, textStatus, errorThrown) {
    		    window.location.href = "${pageContext.request.contextPath}/forward/home";
    		}
    	},
    	{
    		param: JSON.stringify({
    		    tender_id: tenderId,
    		    member_login_name: memberLoginName
    		})
    	});
    }
    
    function quote() {
        window.location.href = "${pageContext.request.contextPath}/forward/tender_quote?tenderId=" + tenderId;
    }
});
</script>
</head>
<body>
  <div style="text-align: center;" align="center">
    <jsp:include page="top.jsp" />
    <div id="content" style="width: 1000px; margin: 0 auto; margin-top: 10px; border: 1px solid #eee;">
      <div style="width: 990px; height: 30px; border-bottom: 1px solid #eee; text-align: left; padding-top: 5px; padding-left: 10px;">
        <label class="title_label" style="margin-top: 5px;">招标信息&nbsp;</label>
        <label style="font-size: 13px; height: 30px; vertical-align: middle; font-weight: bold; margin-top: 5px;">&gt;</label>
        <label id="lbl_tender_title" style="font-size: 13px; height: 30px; vertical-align: middle; font-weight: bold; margin-top: 5px;"></label>
      </div>
      <div style="width: 727px; margin: 0 auto; margin-top: 10px; height: 100px; text-align: left;">
        <div style="float: left; width: 510px;">
          <div style="height: 50px;"><label id="tender_title" style="font-size: 20px; font-weight: bold;"></label></div>
          <div style="margin-top: 5px; font-size: 16px; font-weight: bold;">距离<span id="timer_type"></span>还有
            <label id="timer_day" style="color: red;">0</label>&nbsp;<label style="color: blue;">天</label>
            <label id="timer_hour" style="color: red;">00</label>&nbsp;<label style="color: blue;">小时</label>
            <label id="timer_minute" style="color: red;">00</label>&nbsp;<label style="color: blue;">分</label>
            <label id="timer_second" style="color: red;">00</label>&nbsp;<label style="color: blue;">秒</label>
          </div>
          <div style="margin-top: 5px; font-size: 15px; font-weight: bold; color: #999;">已有
            <label id="vender_count"></label>
            <label>个供应商<span id="action_name"></span></label>
          </div>
        </div>
        <div style="float: left; text-align: center; vertical-align: middle; width: 200px; height: 100px;">
          <button type="button" id="btn_attend" class="enroll_wait" style="margin-top: 15px;"></button>
        </div>
      </div>
      <div style="width: 727px; margin: 0 auto; margin-top: 20px;">
        <table class="info_table" style="margin-top: 5px;" >
          <tr>
            <th width="120">招标编号</th>
            <td width="120"><label id="tender_no"></label></td>
            <th width="120">招标方式</th>
            <td width="120"><label id="tender_pattern_name"></label></td>
            <th width="120">发布日期</th>
            <td width="120"><label id="create_date"></label></td>
          </tr>
          <tr>
            <th width="120">报名开始日</th>
            <td width="120"><label id="enroll_start_date"></label></td>
            <th width="120">报名截止日</th>
            <td width="120"><label id="enroll_end_date"></label></td>
            <th width="120">投标截止日</th>
            <td width="120"><label id="tender_end_date"></label></td>
          </tr>
          <tr>
            <th width="120">中标公布日</th>
            <td width="120"><label id="publish_result_date"></label></td>
            <th width="120">联系人</th>
            <td width="120"><label id="contact_name"></label></td>
            <th width="120">联系电话</th>
            <td width="120"><label id="contact_tel"></label></td>
          </tr>
        </table>
      </div>
      <div style="width: 727px; margin: 0 auto; margin-top: 20px; text-align: left;">
        <label class="common_label">招标产品</label>
        <table id="tender_product" class="display">
          <thead>
            <tr>
              <th>产品名称</th>
              <th>数量</th>
              <th>单位</th>
              <th>产品描述</th>
            </tr>
          </thead>
        </table>
      </div>
      <div style="width: 727px; margin: 0 auto; margin-top: 20px; text-align: left;">
        <label class="common_label">内容说明</label>
        <p><span id="content_description"></span></p>
      </div>
      <div style="width: 727px; margin: 0 auto; margin-top: 20px; text-align: left;">
        <label class="common_label">供应商要求</label>
        <p><span id="vendor_requirement"></span></p>
      </div>
    </div>
    <div id="footer"><s:text name="copyright" /></div>
  </div>
</body>
</html>